//
//  chatuserdb.m
//  tcpoctest
//
//  Created by apple on 13-3-20.
//  Copyright (c) 2013年 gump. All rights reserved.
//

#import "chatuserdb.h"
#import "ChatContentInfo.h"
#import "chatDataSource.h"

@implementation chatuserdb

-(void)creatTable
{
    if (db == nil) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    [db executeUpdate:@"create table if not exists chatuser(mail text,\
firendmail text,isgroup char(1))"];
    [db executeUpdate:@"create table if not exists chatrecord(mail text,\
firendmail text,chattype char(1),chatcontent text,isself char(1),isgroup char(1),\
audiolength float,chattime text,Sequence int,giftid text,giftnum int,sendmail text,\
chatid text,statetype int)"];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)initData
{
    if (db == nil) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    NSString *strsql;
    
    FMResultSet *s;
    
    //init single chat
    strsql = [NSString stringWithFormat:@"SELECT firendmail \
FROM chatuser where mail = '%@' and isgroup = '0'",[DataControl shareControl].personDS.uinfo.mail];
    
    s = [db executeQuery:strsql];
    
    [[DataControl shareControl].chatDS RemoveAllChatDic];
    
    [DataControl shareControl].chatDS.currentChatArr = nil;
    
    NSMutableArray *tempchatarr = [[NSMutableArray alloc] init];
    
    while ([s next])
    {
        NSString *chatmail = [s stringForColumn:@"firendmail"];
        
        NSMutableArray *chatarr = [[NSMutableArray alloc] init];
        [[DataControl shareControl].chatDS SetChatDicArr:chatarr withKey:chatmail];
        
        [tempchatarr addObject:chatmail];
    }
    
    for (NSString *m in tempchatarr)
    {
        strsql = [NSString stringWithFormat:@"SELECT chattype,chatcontent,isself,audiolength,chattime,\
Sequence,giftid,giftnum,sendmail,chatid,statetype \
FROM chatrecord where mail = '%@' and isgroup = '0' and firendmail = '%@'",
                  [DataControl shareControl].personDS.uinfo.mail
                  ,m];
        
        s = [db executeQuery:strsql];
        
        
            
            NSMutableArray *arr = [[DataControl shareControl].chatDS GetArrByKey:m];
            if (arr != nil)
            {
                
                while ([s next])
                {
                    ChatContentInfo *ci = [[ChatContentInfo alloc] init];
                    ci.mail = m;
                    ci.chatType = [s intForColumn:@"chattype"];
                    ci.strtext = [s stringForColumn:@"chatcontent"];
                    ci.isSelf = [s intForColumn:@"isself"];
                    ci.audioPlayLength = [s doubleForColumn:@"audiolength"];
                    ci.isgroup = false;
                    ci.strChatDate = [s stringForColumn:@"chattime"];
                    ci.iSequence = [s intForColumn:@"Sequence"];
                    ci.giftid = [s stringForColumn:@"giftid"];
                    ci.igiftNum = [s intForColumn:@"giftnum"];
                    ci.strsendmail = [s stringForColumn:@"sendmail"];
                    ci.strid = [s stringForColumn:@"chatid"];
                    ci.ichatReadState = [s intForColumn:@"statetype"];
                    [arr addObject:ci];
                    
                    [[DataControl shareControl].chatDS addChatInfo:ci withSquence:ci.iSequence];
                    [[DataControl shareControl].chatDS addChatInfo:ci withID:ci.strid];
                }
            }
       
        
        
    }
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)insertChatDic:(NSString *)mail withIsGroup:(bool)isgroup
{
    if (db == nil) {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:@"INSERT INTO chatuser \
(mail,firendmail,isgroup)values('%@','%@','%d')"
                        ,[DataControl shareControl].personDS.uinfo.mail
                        ,mail
                        ,isgroup];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    [db executeUpdate:strsql];
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)deleteChatDic:(NSString *)mail withIsGroup:(bool)isgroup
{
    if (db == nil) {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:@"DELETE FROM chatuser \
where mail = '%@' and firendmail = '%@' and isgroup = '%d'"
                        ,[DataControl shareControl].personDS.uinfo.mail
                        ,mail
                        ,isgroup];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    [db executeUpdate:strsql];
    
    strsql = [NSString stringWithFormat:@"DELETE FROM chatrecord \
where mail = '%@' and firendmail = '%@' and isgroup = '%d'"
              ,[DataControl shareControl].personDS.uinfo.mail
              ,mail
              ,isgroup];
    
    [db executeUpdate:strsql];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)insertChatInfo:(ChatContentInfo *)cinfo
{
    if (db == nil) {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:@"INSERT INTO chatrecord \
(mail,firendmail,isgroup,chattype,chatcontent,isself,\
audiolength,chattime,Sequence,giftid,giftnum,sendmail,chatid,statetype)\
values('%@','%@','%d','%d','%@','%d',%f,'%@',%d,'%@',%d,'%@','%@',%d)"
                        ,[DataControl shareControl].personDS.uinfo.mail
                        ,cinfo.mail
                        ,cinfo.isgroup
                        ,cinfo.chatType
                        ,cinfo.strtext
                        ,cinfo.isSelf
                        ,cinfo.audioPlayLength
                        ,cinfo.strChatDate
                        ,cinfo.iSequence
                        ,cinfo.giftid
                        ,cinfo.igiftNum
                        ,cinfo.strsendmail
                        ,cinfo.strid
                        ,cinfo.ichatReadState];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    [db executeUpdate:strsql];
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)UpdateChatText:(ChatContentInfo *)cinfo
{
    if (db == nil) {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:@"update chatrecord \
set chatcontent = '%@' \
WHERE mail = '%@' and firendmail = '%@' and isgroup = '%d' \
and chattime = '%@' and chattype = '%d' and Sequence = %d"
              ,cinfo.strtext
              ,[DataControl shareControl].personDS.uinfo.mail
                        ,cinfo.mail
                        ,cinfo.isgroup
                        ,cinfo.strChatDate
                        ,cinfo.chatType
                        ,cinfo.iSequence];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    [db executeUpdate:strsql];
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);

}

-(void)UpdateChatID:(ChatContentInfo *)cinfo
{
    if (db == nil) {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:@"update chatrecord \
set chatid = '%@',chattime = '%@' \
WHERE mail = '%@' and firendmail = '%@' and isgroup = '%d' \
and chattype = '%d' and Sequence = %d"
                        ,cinfo.strid
                        ,cinfo.strChatDate
                        ,[DataControl shareControl].personDS.uinfo.mail
                        ,cinfo.mail
                        ,cinfo.isgroup
                        ,cinfo.chatType
                        ,cinfo.iSequence];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    [db executeUpdate:strsql];
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
    
}

-(void)UpdateChatState:(ChatContentInfo *)cinfo
{
    if (db == nil) {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:@"update chatrecord \
set statetype = %d \
WHERE mail = '%@' and firendmail = '%@' and isgroup = '%d' \
and chattype = '%d' and Sequence = %d"
                        ,cinfo.ichatReadState
                        ,[DataControl shareControl].personDS.uinfo.mail
                        ,cinfo.mail
                        ,cinfo.isgroup
                        ,cinfo.chatType
                        ,cinfo.iSequence];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    [db executeUpdate:strsql];
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
    
}

-(void)deleChatInfo:(ChatContentInfo *)cinfo
{
    if (db == nil) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    NSString *strsql = [NSString stringWithFormat:@"DELETE FROM chatrecord \
where mail = '%@' and firendmail = '%@' and isgroup = '%d' and \
chattype = '%d' and chatcontent = '%@' and isself = '%d' and \
audiolength = %f and chattime = '%@' and Sequence = %d"
              ,[DataControl shareControl].personDS.uinfo.mail
              ,cinfo.mail
              ,cinfo.isgroup
              ,cinfo.chatType
              ,cinfo.strtext
              ,cinfo.isSelf
              ,cinfo.audioPlayLength
              ,cinfo.strChatDate
              ,cinfo.iSequence];
    
    [db executeUpdate:strsql];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

@end
